{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['', '/home/csunix/schtmt/.local/lib/python2.7/site-packages/cudamat-0.3-py2.7-linux-x86_64.egg', '/home/cserv1_a/soc_pg/schtmt/NewFolder/caffe_May2017/examples/1_H_examples', '/home/cserv1_a/soc_pg/schtmt/NewFolder/caffe_May2017/examples/1_H_examples/usr/not-backed-up/unsupervised-videos-master/cudamat', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python27.zip', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/plat-linux2', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/lib-tk', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/lib-old', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/lib-dynload', '/home/csunix/schtmt/.local/lib/python2.7/site-packages', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/site-packages', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/site-packages/Sphinx-1.4.1-py2.7.egg', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/site-packages/setuptools-23.0.0-py2.7.egg', '/home/csunix/linux/apps/install/anaconda/4.1.1/lib/python2.7/site-packages/IPython/extensions', '/home/cserv1_a/soc_pg/schtmt/.ipython']\n"
     ]
    },
    {
     "ename": "ImportError",
     "evalue": "No module named google.protobuf.internal",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mImportError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-4a7f9cd29a51>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmatplotlib\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpyplot\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mplt\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mcaffe\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/csunix/schtmt/NewFolder/caffe_May2017/python/caffe/__init__.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0mpycaffe\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mNet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSGDSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNesterovSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdaGradSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mRMSPropSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdaDeltaSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdamSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNCCL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTimer\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0m_caffe\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0minit_log\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlog\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_mode_cpu\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_mode_gpu\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_device\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mLayer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mget_solver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlayer_type_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_random_seed\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msolver_count\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_solver_count\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msolver_rank\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_solver_rank\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mset_multiprocess\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhas_nccl\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0m_caffe\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0m__version__\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0mproto\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcaffe_pb2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mTRAIN\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTEST\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0mclassifier\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mClassifier\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/csunix/schtmt/NewFolder/caffe_May2017/python/caffe/pycaffe.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[1;33m.\u001b[0m\u001b[0m_caffe\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mNet\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mSGDSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNesterovSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdaGradSolver\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0;31m \u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     14\u001b[0m         \u001b[0mRMSPropSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdaDeltaSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mAdamSolver\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNCCL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTimer\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 15\u001b[1;33m \u001b[1;32mimport\u001b[0m \u001b[0mcaffe\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mio\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     17\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0msix\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/csunix/schtmt/NewFolder/caffe_May2017/python/caffe/io.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      7\u001b[0m     \u001b[1;31m# Python3 will most likely not be able to load protobuf\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m     \u001b[1;32mfrom\u001b[0m \u001b[0mcaffe\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mproto\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mcaffe_pb2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      9\u001b[0m \u001b[1;32mexcept\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     10\u001b[0m     \u001b[1;32mimport\u001b[0m \u001b[0msys\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m/home/csunix/schtmt/NewFolder/caffe_May2017/python/caffe/proto/caffe_pb2.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;31m# source: caffe.proto\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m \u001b[1;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprotobuf\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minternal\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0menum_type_wrapper\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprotobuf\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mdescriptor\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0m_descriptor\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[1;32mfrom\u001b[0m \u001b[0mgoogle\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mprotobuf\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mmessage\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0m_message\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mImportError\u001b[0m: No module named google.protobuf.internal"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print sys.path\n",
    "#sys.path.insert(0,'/home/csunix/schtmt/NewFolder/caffe_May2017/python')\n",
    "sys.path.append('/home/csunix/schtmt/NewFolder/caffe_May2017/python')\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import caffe\n",
    "def visualize_weights(net, layer_name, padding=4, filename=''):\n",
    "    # The parameters are a list of [weights, biases]\n",
    "    data = np.copy(net.params[layer_name][0].data)\n",
    "    # N is the total number of convolutions\n",
    "    N = data.shape[0]*data.shape[1]\n",
    "    # Ensure the resulting image is square\n",
    "    filters_per_row = int(np.ceil(np.sqrt(N)))\n",
    "    # Assume the filters are square\n",
    "    filter_size = data.shape[2]\n",
    "    # Size of the result image including padding\n",
    "    result_size = filters_per_row*(filter_size + padding) - padding\n",
    "    # Initialize result image to all zeros\n",
    "    result = np.zeros((result_size, result_size))\n",
    " \n",
    "    # Tile the filters into the result image\n",
    "    filter_x = 0\n",
    "    filter_y = 0\n",
    "    for n in range(data.shape[0]):\n",
    "        for c in range(data.shape[1]):\n",
    "            if filter_x == filters_per_row:\n",
    "                filter_y += 1\n",
    "                filter_x = 0\n",
    "            for i in range(filter_size):\n",
    "                for j in range(filter_size):\n",
    "                    result[filter_y*(filter_size + padding) + i, filter_x*(filter_size + padding) + j] = data[n, c, i, j]\n",
    "            filter_x += 1\n",
    " \n",
    "    # Normalize image to 0-1\n",
    "    min = result.min()\n",
    "    max = result.max()\n",
    "    result = (result - min) / (max - min)\n",
    " \n",
    "    # Plot figure\n",
    "    plt.figure(figsize=(10, 10))\n",
    "    plt.axis('off')\n",
    "    plt.imshow(result, cmap='gray', interpolation='nearest')\n",
    " \n",
    " \n",
    "    # Save plot if filename is set\n",
    "    if filename != '':\n",
    "        plt.savefig(filename, bbox_inches='tight', pad_inches=0)\n",
    " \n",
    "    plt.show()\n",
    "\n",
    "# Load model\n",
    "net = caffe.Net('/home/csunix/schtmt/NewFolder/caffe_May2017/examples/1_H_examples/mnist_wta_autoencoder/mnist_wta_autoencoder.prototxt',\n",
    "                '/usr/not-backed-up/1_convlstm/mnist_wta_AE/mnist_wta_autoencoder_iter_6000.caffemodel',\n",
    "                caffe.TEST)\n",
    "\n",
    " \n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "visualize_weights(net, 'conv1', filename='conv1.png')   "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [Root]",
   "language": "python",
   "name": "Python [Root]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
